class Auth {
    constructor() {
        this.loginForm = document.getElementById('loginForm');
        this.registerForm = document.getElementById('registerForm');
        
        this.init();
    }
    
    init() {
        if (this.loginForm) {
            this.loginForm.addEventListener('submit', (e) => this.handleLogin(e));
        }
        
        if (this.registerForm) {
            this.registerForm.addEventListener('submit', (e) => this.handleRegister(e));
        }
    }
    
    handleLogin(e) {
        e.preventDefault();
        
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;
        let isValid = true;
        
        // 验证用户名
        if (!username) {
            this.showError('usernameError', '请输入用户名');
            isValid = false;
        } else {
            this.hideError('usernameError');
        }
        
        // 验证密码
        if (!password) {
            this.showError('passwordError', '请输入密码');
            isValid = false;
        } else if (password.length < 6) {
            this.showError('passwordError', '密码长度不能少于6位');
            isValid = false;
        } else {
            this.hideError('passwordError');
        }
        
        if (isValid) {
            // 模拟登录请求
            setTimeout(() => {
                this.showNotification('登录成功！');
                window.location.href = 'index.html';
            }, 500);
        }
    }
    
    handleRegister(e) {
        e.preventDefault();
        
        const username = document.getElementById('reg-username').value;
        const phone = document.getElementById('reg-phone').value;
        const password = document.getElementById('reg-password').value;
        const confirm = document.getElementById('reg-confirm').value;
        let isValid = true;
        
        // 验证用户名
        if (!username) {
            this.showError('usernameError', '请输入用户名');
            isValid = false;
        } else if (username.length < 4) {
            this.showError('usernameError', '用户名至少4个字符');
            isValid = false;
        } else {
            this.hideError('usernameError');
        }
        
        // 验证手机号
        if (!phone) {
            this.showError('phoneError', '请输入手机号');
            isValid = false;
        } else if (!/^1[3-9]\d{9}$/.test(phone)) {
            this.showError('phoneError', '请输入正确的手机号');
            isValid = false;
        } else {
            this.hideError('phoneError');
        }
        
        // 验证密码
        if (!password) {
            this.showError('passwordError', '请输入密码');
            isValid = false;
        } else if (password.length < 6) {
            this.showError('passwordError', '密码长度不能少于6位');
            isValid = false;
        } else {
            this.hideError('passwordError');
        }
        
        // 验证确认密码
        if (!confirm) {
            this.showError('confirmError', '请确认密码');
            isValid = false;
        } else if (password !== confirm) {
            this.showError('confirmError', '两次密码不一致');
            isValid = false;
        } else {
            this.hideError('confirmError');
        }
        
        if (isValid) {
            // 模拟注册请求
            setTimeout(() => {
                this.showNotification('注册成功！');
                window.location.href = 'login.html';
            }, 500);
        }
    }
    
    showError(id, message) {
        const errorEl = document.getElementById(id);
        errorEl.textContent = message;
        errorEl.style.display = 'block';
    }
    
    hideError(id) {
        const errorEl = document.getElementById(id);
        errorEl.style.display = 'none';
    }
    
    showNotification(message) {
        const notification = document.createElement('div');
        notification.className = 'notification show';
        notification.textContent = message;
        document.body.appendChild(notification);
        
        setTimeout(() => {
            notification.classList.add('fade-out');
            setTimeout(() => {
                notification.remove();
            }, 500);
        }, 2000);
    }
}

// 初始化认证
document.addEventListener('DOMContentLoaded', () => {
    new Auth();
});